package ds.safelock.db;

public class KeysDBHelper {

	protected static final double VERSION = 1.0;
	
	private org.hsqldb.server.Server server;
	private java.sql.Connection connection;
	
	public KeysDBHelper() throws java.sql.SQLException, ClassNotFoundException
	{ 
		//load the engine
		Class.forName("org.hsqldb.jdbc.JDBCDriver");
		//init the server
		server = new org.hsqldb.server.Server();
		server.setTls(true);
		//init the connection
		connection = java.sql.DriverManager.getConnection("jdbc:hsqldb:file:"+ds.safelock.config.Configuration.HOME+
															ds.safelock.config.Configuration.DATABASE_NAME);

		//create the tables if applicable
		//ignore exceptions, means the tables are already there since the SQL syntax is fine
		java.sql.Statement st = connection.createStatement();
		try { st.executeUpdate(Keys.TABLE_CREATE); }
		catch (java.sql.SQLException e) { e.printStackTrace(); }		
		st.close();
	}
	
	@Deprecated
	public void open()
	{ }
	
	public void shutdown() throws java.sql.SQLException
	{		
		java.sql.Statement st = connection.createStatement();
        st.execute("SHUTDOWN");
        st.close();
		connection.close();
	}

	/**
	public void insertUser(String username, String key) throws java.sql.SQLException
	{
		byte[] salt;
		try { salt = security.Salt.getSalt(key.toCharArray()); } 
		catch (Exception e) { throw new java.sql.SQLException("Unable to insert user: "+e.getMessage()); }
		
		final byte[][] encoded_user = security.Cipher.cipher(username.getBytes(), key.toCharArray(), salt);
		if (encoded_user == null)
			throw new java.sql.SQLException("Unable to insert user: An error occurred while ciphering the username");
		final byte[][] encoded_key = security.Cipher.cipher(key.getBytes(), key.toCharArray(), salt);
		if (encoded_key == null)
			throw new java.sql.SQLException("Unable to insert user: An error occurred while ciphering the key");

		String[] euser = {security.Base64.encodeBytes(encoded_user[0]),
				security.Base64.encodeBytes(encoded_user[1])};
		String[] ekey = {security.Base64.encodeBytes(encoded_key[0]),
				security.Base64.encodeBytes(encoded_key[1])};
		java.util.Arrays.fill(encoded_user, null);
		java.util.Arrays.fill(encoded_key, null);
		java.util.Arrays.fill(salt, (byte) 0x00);

		java.sql.Array _user = this.connection.createArrayOf("VARCHAR", euser);
		java.util.Arrays.fill(euser, null);
		java.sql.Array _key = this.connection.createArrayOf("VARCHAR", ekey);
		java.util.Arrays.fill(ekey, null);

		System.out.println(Users.TABLE_INSERT);
		java.sql.PreparedStatement ps = this.connection.prepareStatement(Users.TABLE_INSERT);
		ps.setArray(1,_user);
		ps.setArray(2,_key);
		ps.execute();
		ps.close();
		_user.free();
		_key.free();
	}
	
	public void printUsers() throws java.sql.SQLException
	{
		java.sql.Statement st = connection.createStatement();
		java.sql.ResultSet rs = st.executeQuery("select * from users;");
        while (rs.next()) {
            java.sql.Array _user = rs.getArray(Users.USER);
            java.sql.Array _key = rs.getArray(Users.SAFECODE);
            Object[] user = (Object[]) _user.getArray();
            _user.free();
            Object[] key = (Object[])_key.getArray();
            _key.free();
            
            System.out.println("username = "+user[0]);
            System.out.println("iv = "+user[1]);
            System.out.println("key = "+key[0]);          
            System.out.println("iv = "+key[1]);          
    		java.util.Arrays.fill(user, (byte) 0x00);
    		java.util.Arrays.fill(key, (byte) 0x00);
        }
        rs.close();
        st.close();
	}
	**/
	
}
